<?php

/*
 * 仓库管理
 * @author wyb
 */

namespace Seller\Controller;

class ExpressController extends BaseController {

    private $_model = null;

    public function _initialize() {
        parent::_initialize();
        $this->_model = M('logistics_needs');
    }

    /**
     * 添加在线发布物流单子
     */
    public function add() {
        if ($this->__get['ref']){
            $needinfo = $this->_model->where(array('ref'=>$this->__get['ref']))->select();
            if (!$needinfo){
                $needinfo = M('order_line')->field('ref,supply_product_id')->where(array('ref'=>$this->__get['ref']))->select();
            }
            foreach ($needinfo as $nk=>$nv){
                $supply_customer_id = M('order_line')->where(array('supply_product_id'=>$nv['supply_product_id']))->getField('supply_customer_id');
                $needinfo[$nk]['customer_name'] = M('customer')->where(array('id'=>$supply_customer_id))->getField('shop_name');
            }
        }else{
            $this->error("订单号不存在");
        }
        $orderinfo = M('order')->where(array('ref'=>$this->__get['ref']))->find();

        $goodtotalprice = $orderinfo['amount_total']-$orderinfo['delivery_fee'];
        if (IS_POST) {

            foreach ($this->__post as $key=>$val){

                foreach ($val as $k=>$v){
                    $insertdata[$k][$key] = $v;
                }
                if (count($val)==1){
                    $insertdata[1][$key] = $val[0];
                }
            }
            $fee = 0;
            $m = count($insertdata);
            $n = 0;
            foreach ($insertdata as $ik=>$insertarr){
                $insertarr['userid'] = $this->__seller_info['id'];
                $insertarr['province_name'] = M('area')->where(array('areaid'=>$insertarr['province_id']))->getField('areaname');
                $insertarr['city_name'] = M('area')->where(array('areaid'=>$insertarr['city_id']))->getField('areaname');
                $insertarr['area_name'] = M('area')->where(array('areaid'=>$insertarr['area_id']))->getField('areaname');
                $fee += $insertarr['price'];
                if($insertarr['id']){
                    if ($this->_model->save($insertarr)){
                        $n += 1;
                        continue;
                    }else{
                        break;
                    }
                }else{
                    if ($this->_model->add($insertarr)) {

                        $n += 1;
                        continue;
                    } else {
                        break;
                    }
                }

            }

            $orderinfo['delivery_fee'] = $fee;
            $orderinfo['amount_total'] = $goodtotalprice+$fee;

            M('order')->save($orderinfo);
            if ($n == $m){
                $this->success('发布成功',U('Order/detail',array('type' => 'fx', 'ref' => $insertarr['ref'])));
            }else{
                $this->error('有'.$m-$n.'条失败');
            }
        }
        if ($this->__get['ref']){
            $needinfo = $this->_model->where(array('ref'=>$this->__get['ref']))->select();
            if (!$needinfo){
                $needinfo = M('order_line')->field('ref,supply_product_id')->where(array('ref'=>$this->__get['ref']))->select();
            }
            foreach ($needinfo as $nk=>$nv){
                $supply_customer_id = M('order_line')->where(array('supply_product_id'=>$nv['supply_product_id']))->getField('supply_customer_id');
                $needinfo[$nk]['customer_name'] = M('customer')->where(array('id'=>$supply_customer_id))->getField('shop_name');
            }
        }else{
            $this->error("订单号不存在");
        }

        $this->assign('needinfo',$needinfo);
        $this->display('form');
    }

    public function area() {
        $area = M("area")->field("areaid as id,areaname as name,parent_id")->select();
        $area_data = array();
        foreach( $area as $k => $v ) {
            $area_data[$v['parent_id']][] = $v;
        }
        $area = $area_data[0];
        foreach( $area as $key => $val ) {
            $area_info = '';
            if( isset( $area_data[$val['id']] ) ) {
                $area_info =  $area_data[$val['id']];
                foreach( $area_info as $k => $v ) {
                    $area_level = array();
                    if( isset( $area_data[$v['id']] ) ) {
                        $area_level = $area_data[$v['id']];
                    }
                    $area_info[$k]['sub'] = $area_level;
                }
            }
            $area[$key]['sub'] = $area_info;
        }
        $this->success($area);
    }
}
